﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WorkBasket.Data.Entities;
using WorkBasket.Data.Repository;
using WorkBasket.Infrastructure;
using WorkBasket.Models;

namespace WorkBasket.Controllers.Api
{
    public class GroupController : ApiController
    {
        SOC soc = new SOC(new MyContext());
        [Route("api/group/GetByParentKey/{UnitID}")]
        [HttpGet]
        public IHttpActionResult GetByParentKey(int UnitID)
        {
      
            return Json(soc.Groups.Where(c => c.UnitID == UnitID));
        }

        [HttpPost]
        [Route("api/group/delete")]
        public IHttpActionResult Delete([FromBody] Group value)
        {
            try
            {
                soc.Groups.Delete(value.GroupID);
                soc.CommitChanges();
                return Json(new DataReturn("Group Deleted", value));
            }
            catch (Exception e)
            {
                return new System.Web.Http.Results.ExceptionResult(e, this);
            }
        }

        [HttpPost]
        [Route("api/group/save")]
        public IHttpActionResult Post([FromBody] Group value)
        {
            try
            {
                if (value.GroupID == 0)
                {
                    soc.Groups.Add(value);
                }
                else
                {
                    soc.Groups.Update(value);
                }

                soc.CommitChanges();
                return Json(new DataReturn(value.GroupName + " was Saved", value));
            }
            catch (Exception e)
            {
                return new System.Web.Http.Results.ExceptionResult(e, this);
            }
        }


    }
}
